(function(angular) {
	"use strict";
	angular.module("fs.waf").factory("loanPastdueService", loanPastdueService);
	loanPastdueService.$inject = [ "modalService", "arithService" ];
	function loanPastdueService(modalService, arithService) {

		function resetPastdueTenorFrom(pastdueSchdList, selectedPastdues) {
			if (pastdueSchdList) {
				// reset pastdueTenorFrom
				var pastdueTenorFrom = 1;
				angular.forEach(pastdueSchdList, function(pastdue, index) {
					var selectedFlag = false;
					if (selectedPastdues) {
						// filter out have been selected pastdueSchd
						for (var i = 0; i < selectedPastdues.length; i++) {
							var selectedPastdue = selectedPastdues[i];
							if (pastdue.$$hashKey == selectedPastdue.$$hashKey) {
								selectedFlag = true;
								break;
							}
						}
					}
					if (!selectedFlag) {
						pastdue.pastdueTenorFrom = pastdueTenorFrom;
						pastdueTenorFrom = arithService.add(pastdue.pastdueTenor, 1);
					}
				});
			}
		}

		function resetPastdueCustTierTenorFrom(tierBaseRateCusts, pastDueSelectedCustTiers) {
			if (tierBaseRateCusts) {
				// reset pastdueTenorFrom
				var custTierTenorFrom = 0;
				angular.forEach(tierBaseRateCusts, function(pastdue, index) {
					var selectedFlag = false;
					if (pastDueSelectedCustTiers) {
						// filter out have been selected pastdueSchd
						for (var i = 0; i < pastDueSelectedCustTiers.length; i++) {
							var selectedCustTiers = pastDueSelectedCustTiers[i];
							if (pastdue.$$hashKey == selectedCustTiers.$$hashKey) {
								selectedFlag = true;
								break;
							}
						}
					}
					if (!selectedFlag) {
						pastdue.tenerFrom = custTierTenorFrom;
						custTierTenorFrom = pastdue.tener;
					}
				});
			}
		}

		function addPastdue(row, pastdueSchdList) {
			if (!pastdueSchdList) {
				pastdueSchdList = [];
			}
			var pastdueTenorFrom;
			if (pastdueSchdList.length > 0) {
				var prePastdueTenor = pastdueSchdList[pastdueSchdList.length - 1].pastdueTenor;
				// if(!prePastdueTenor){
				// modalService.openErrorDialogs({
				// messages : [ "error.info.todaymustbenotnull" ]
				// });
				// return false;
				// }
				if (prePastdueTenor != undefined && prePastdueTenor != "") {
					pastdueTenorFrom = arithService.add(prePastdueTenor, 1);
				}

			} else {
				pastdueTenorFrom = 1;
			}
			row.pastdueTenorFrom = pastdueTenorFrom;
			return true;
		}
		;

		function addPastdueCustTier(row, tierBaseRateCusts) {
			if (!tierBaseRateCusts) {
				tierBaseRateCusts = [];
			}
			var tenerFrom;
			if (tierBaseRateCusts.length > 0) {
				tenerFrom = tierBaseRateCusts[tierBaseRateCusts.length - 1].tener;
			} else {
				tenerFrom = 0;
			}
			row.tenerFrom = tenerFrom;
		}

		function custTierMoreDataDyna(obj) {
			var params = {};
			// for get dynaparm page
			params.parmSubcat = obj.alg;
			// for data on edit page popup
			params.custDynaparm = {};
			params.custDynaparm.dynamicParams = obj.dynamicParams;
			modalService.openCustomDialogs({
				"title" : "title.popup.custdynaparm",
				"dialogsModule" : "cda.custdynaparm.popupedit",
				"params" : params,
				"btnCallback" : {
					"ok" : function(resultList) {
						obj.dynamicParams = resultList.dynamicParams;
					}
				}
			});
		}

		function pastdueTenorChanged(pastdueSchdList, rowObj) {
			var rowFlag = false, prePastdueTenor, pastdueTenor, len = pastdueSchdList.length;
			for (var i = 0; i < len; i++) {
				var pastdue = pastdueSchdList[i];
				if (i == 0) {
					pastdue.pastdueTenorFrom = 1;
				} else {
					pastdueTenor = pastdue.pastdueTenor;
					var prePastdue = pastdueSchdList[i - 1];
					prePastdueTenor = prePastdue.pastdueTenor;

					if (i == (len - 1) && !pastdueTenor) {
						return;
					}
					if (!arithService.gt(pastdueTenor, prePastdueTenor)) {
						modalService.openErrorDialogs({
							messages : [ {
								msg : "error.info.tonthday-bigger",
								args : [ (i + 1), i ]
							} ]
						});
						return;
					}

					if (i < (len - 1)) {
						var nextPastdue = pastdueSchdList[i + 1];
						var nextPastdueTenor = nextPastdue.pastdueTenor;
						if (nextPastdueTenor && !arithService.gt(nextPastdueTenor, pastdueTenor)) {
							modalService.openErrorDialogs({
								messages : [ {
									msg : "error.info.tonthday-smaller",
									args : [ (i + 1), (i + 2) ]
								} ]
							});
							return;
						}
						nextPastdue.pastdueTenorFrom = arithService.add(pastdueTenor, 1);
					}
				}
				if (rowFlag) {
					pastdue.pastdueTenorFrom = arithService.add(prePastdueTenor, 1);
					break;
				}
				if (pastdue.$$hashKey == rowObj.$$hashKey) {
					prePastdueTenor = pastdue.pastdueTenor;
					rowFlag = true;
					if (pastdue.pastdueGrace) {
						if (!arithService.gt(prePastdueTenor, pastdue.pastdueGrace)) {
							modalService.openErrorDialogs({
								messages : [ {
									msg : "error.info.graceperiod-tenor",
									args : [ (i + 1), (i + 1) ]
								} ]
							});
							return;
						}
					}
				}

			}
		}

		function pastdueGraceChanged(pastdueSchdList, value) {
			var len = pastdueSchdList.length;
			for (var i = 0; i < len; i++) {
				var pastdue = pastdueSchdList[i];
				if (pastdue.$$hashKey == value.$$hashKey && pastdue.pastdueTenor) {
					pastdue.pastdueGrace = pastdue.pastdueGrace ? pastdue.pastdueGrace : 0;
					var pastdueGrace = pastdue.pastdueGrace;
					var pastdueTenor = pastdue.pastdueTenor;
					if (i == 0) {
						if (!arithService.gt(pastdueTenor, pastdueGrace)) {
							modalService.openErrorDialogs({
								messages : [ {
									msg : "error.info.graceperiod-tenor",
									args : [ (i + 1), (i + 1) ]
								} ]
							});
							return;
						}
					} else {
						var prePastdue = pastdueSchdList[i - 1];
						var prePastdueTenor = prePastdue.pastdueTenor;

						if (i == (len - 1) && !pastdueTenor) {
							return;
						}
						if (!arithService.gt(arithService.sub(pastdueTenor, prePastdueTenor), pastdueGrace)) {
							modalService.openErrorDialogs({
								messages : [ {
									msg : "error.info.graceperiod-today",
									args : [ (i + 1), (i + 1), i ]
								} ]
							});
							return;
						}
					}
				}
			}
		}

		function resetIntByIntBaseType(loan) {
			var intBaseCustomAlgData = [];
			for (var i = 0; i < loan.allIntBaseCustomAlgs.length; i++) {
				var _intBaseCustomAlg = loan.allIntBaseCustomAlgs[i];
				var _filterTypes = _intBaseCustomAlg.extProperty.INT_BASE_TYPES;
				if (_filterTypes && _filterTypes.indexOf(loan.loanPsd.intBaseType) >= 0) {
					intBaseCustomAlgData.push(_intBaseCustomAlg);
				}
			}
			return intBaseCustomAlgData;
		}

		function resetIntRateIndexOptions(loan) {
			// filter intRateIndex by ccy
			var intRateIndexOptions = [];
			for (var i = 0; i < loan.allIntRateIndexOptions.length; i++) {
				var _intIndex = loan.allIntRateIndexOptions[i];
				if (_intIndex.extProperty.ccy == loan.loanInfo.loanCcy) {
					intRateIndexOptions.push(loan.allIntRateIndexOptions[i]);
				}
			}
			loan.intRateIndexOptions = intRateIndexOptions;
			loan.copyIntRateIndexOptions = angular.copy(loan.intRateIndexOptions);
		}

		function resetIntRateIndexByIntBaseType(loan) {
			var resultOptionData = [];
			var resultData = [];
			var rateIndexValueStr = "";
			if (loan.loanPsd.intBaseType == "F") {
				for (var i = 0; i < loan.copyIntRateIndexOptions.length; i++) {
					var intRateIndex = loan.copyIntRateIndexOptions[i];
					var intIndexObj = intRateIndex.extProperty;
					var intIndexCode = intIndexObj.intIndexCode;
					var dataObj = {};
					dataObj.value = intIndexCode;
					// remove the sitecode, like "MDBANK:"
					dataObj.label = intIndexCode.substring(intIndexObj.siteCode.length + intIndexObj.ccy.length + 2, intIndexCode.length);
					resultData.push(dataObj);
				}
			} else if (loan.loanPsd.intBaseType == "N" || loan.loanPsd.intBaseType == "C" || loan.loanPsd.intBaseType == "I") {
				for (var i = 0; i < loan.copyIntRateIndexOptions.length; i++) {
					var intRateIndex = loan.copyIntRateIndexOptions[i];
					var intIndexObj = intRateIndex.extProperty;
					var tenorFlag = intIndexObj.tenorFlag;
					var dataObj = {};
					dataObj.value = intIndexObj.siteCode + ":" + intIndexObj.ccy + ":" + intIndexObj.intIndexName;
					dataObj.label = intIndexObj.intIndexName;
					if (tenorFlag == "Y" && rateIndexValueStr.indexOf(dataObj.value + ",") == -1) {
						rateIndexValueStr += dataObj.value + ",";
						resultData.push(dataObj);
					}
				}
			} else {
				for (var i = 0; i < loan.copyIntRateIndexOptions.length; i++) {
					var intRateIndex = loan.copyIntRateIndexOptions[i];
					var intIndexObj = intRateIndex.extProperty;
					var intIndexCode = intIndexObj.intIndexCode;
					var dataObj = {};
					dataObj.value = intIndexCode;
					// remove the sitecode, like "MDBANK:"
					dataObj.label = intIndexCode.substring(intIndexObj.siteCode.length + intIndexObj.ccy.length + 2, intIndexCode.length);
					resultData.push(dataObj);
				}
			}
			resultOptionData = resultData;
			loan.intRateIndexOptions = resultOptionData;
		}

		function pastDueIntBaseTypeChanged(loan) {
			if (loan.loanPsd && loan.loanPsd.intBaseType) {
				loan.intBaseCustomAlgOptions = resetIntByIntBaseType(loan);
				resetIntRateIndexByIntBaseType(loan);
				// need clear
				if (loan.loanPsd.intBaseType != "C") {
					loan.loanPsd.tierBaseRateCusts = [];
				}
				// need clear
				if (loan.loanPsd.intBaseType != "M") {
					loan.loanPsd.tierBaseRateMixs = [];
				}
			}
		}

		function validateCustTier(loan) {
			if (loan.loanPsd.tierBaseRateCusts && loan.loanPsd.intBaseType == "C") {
				var lastTener;
				for (var i = 0; i < loan.loanPsd.tierBaseRateCusts.length; i++) {
					var tierBaseRateCust = loan.loanPsd.tierBaseRateCusts[i];
					var tenerFrom = tierBaseRateCust.tenerFrom;
					var tener = tierBaseRateCust.tener;
					if (!tener) {
						var msg = "error.info.excludeisrequired";
						return msg;
					}
					if (!tierBaseRateCust.alg) {
						var msg = "error.info.calschemeisrequired";
						return msg;
					}
					if (arithService.lt(tener, tenerFrom)) {// tener<tenerFrom
						var msg = {
							msg : "error.info.excludebiggerfrom",
							args : [ (i + 1) ]
						}
						return msg;
					}
					if (i == 0) {
						lastTener = tener;
						continue;
					}
					var currTenerFromNeedValue = lastTener;
					if (!arithService.eq(tenerFrom, currTenerFromNeedValue)) {
						var msg = {
							msg : "error.info.fromequalpreviousexclude",
							args : [ (i + 1) ]
						}
						return msg;
					}
					lastTener = tener;
				}
			}
		}

		return {
			resetPastdueTenorFrom : resetPastdueTenorFrom,
			resetPastdueCustTierTenorFrom : resetPastdueCustTierTenorFrom,
			addPastdue : addPastdue,
			addPastdueCustTier : addPastdueCustTier,
			custTierMoreDataDyna : custTierMoreDataDyna,
			pastdueTenorChanged : pastdueTenorChanged,
			pastdueGraceChanged : pastdueGraceChanged,
			resetIntByIntBaseType : resetIntByIntBaseType,
			resetIntRateIndexOptions : resetIntRateIndexOptions,
			resetIntRateIndexByIntBaseType : resetIntRateIndexByIntBaseType,
			pastDueIntBaseTypeChanged : pastDueIntBaseTypeChanged,
			validateCustTier : validateCustTier
		};

	}
})(angular);
